﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using BaseTerminalProtocol.Authorizations;
using Helpers;

namespace BaseTerminalProtocol.TerminalActions.Providers
{
    public class GetPaymentStatus
    {
        private static XmlDocument RequestGetPaymentStatus(Authorization pvAuthor, string pvPaymentID)
        {
            // Init XML Document            
            XmlDocument doc = XMLGate.Request_INIT(pvAuthor);
            XmlElement _xml_Root = doc.DocumentElement;
            //provider
            XmlElement _xml_provider = XMLHelper.newElement(doc, "providers");
            //getPaymentStatus
            XmlElement _xml_getPaymentStatus = XMLHelper.newElement(doc, "getPaymentStatus");
            //payment
            XmlElement _xml_payment = XMLHelper.newElement(doc, "payment");
            _xml_payment.Attributes.Append(XMLHelper.newAttribute(doc, "id", pvPaymentID));

            //add to getPaymentStatus
            _xml_getPaymentStatus.AppendChild(_xml_payment);
            // add to provider
            _xml_provider.AppendChild(_xml_getPaymentStatus);
            // add to root
            _xml_Root.AppendChild(_xml_provider);
            return doc;
        }

        public static GateResponseStatus ResponeGetPaymentStatus(Authorization pvAuthor, string pvPaymentID)
        {
            GateResponseStatus response;
            string request = "";

            response = new GateResponseStatus(XMLGate.Request_SEND(RequestGetPaymentStatus(pvAuthor, pvPaymentID), pvAuthor.ServerUrl, out request));
            Tools.Logger("Request: " + request, "info", "RequestAgentID");

            return response;
        }
    }

    public class GateResponseStatus
    {
        private string _strPincode = "";
        public string StrPincode
        {
            get { return _strPincode; }
            set { _strPincode = value; }
        }

        private string _result = "";
        public string Result
        {
            get { return _result; }
            set { _result = value; }
        }

        private string _strSeri = "";
        public string StrSeri
        {
            get { return _strSeri; }
            set { _strSeri = value; }
        }

        private string _strEndDate = "";
        public string StrEndDate
        {
            get { return _strEndDate; }
            set { _strEndDate = value; }
        }

        private string _ResponseData = "";
        public string ResponseDataAtt
        {
            get { return _ResponseData; }
        }

        private string _Status = "";
        public string Status
        {
            get { return _Status; }
        }

        public GateResponseStatus(XmlDocument pvXMLResponse)
        {
            try
            {
                _ResponseData = pvXMLResponse.OuterXml;
                string strResult = pvXMLResponse.GetElementsByTagName("response")[0].Attributes["result"].Value;
                if (strResult == "0")
                {
                    string getPaymentResult = pvXMLResponse.GetElementsByTagName("getPaymentStatus")[0].Attributes["result"].Value;
                    if (getPaymentResult == "0")
                    {
                        string paymentResult = pvXMLResponse.GetElementsByTagName("payment")[0].Attributes["result"].Value;
                        _result = paymentResult;
                        string paymentStatus = pvXMLResponse.GetElementsByTagName("payment")[0].Attributes["status"].Value;
                        _Status = paymentStatus;
                        if (paymentResult == "0" && paymentStatus == "2")
                        {
                            string disp1 = pvXMLResponse.GetElementsByTagName("extras")[0].Attributes["disp1"].Value;
                            string strDecrypt = Tools.Decrypt(Authorization.PassWord, disp1);
                            string[] listItem = strDecrypt.Split('|');
                            _strPincode = listItem[0];
                            _strSeri = listItem[1];
                            _strEndDate = listItem[2];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tools.Logger("Parse Response Error: " + ex.Message, "error", "GateResponse");
                Tools.Logger("Parse Response Error Stack: " + ex.StackTrace, "error", "GateResponse");
                Tools.Logger("Parse Response Error Inner: " + ex.InnerException, "error", "GateResponse");
            }
        }
    }
}
